package airacrobats.gui.container;

import airacrobats.game.Squadron;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

/**
 * This containers has the components for the squadron selection.
 */
public class SquadronSelectionContainer extends JPanel implements ActionListener
{
    /**
     * Combo box with the squadrons to select.
     */
    private JComboBox comboBox;
    /**
     * Image of the selected squadron.
     */
    private JLabel previewImage;
    /**
     * Okay button.
     */
    private JButton button;
    /**
     * Squadrons.
     */
    private List<Squadron> squadrons;

    /**
     * @param actionListener Listener notified when the okay button is pressed.
     * @param squadrons The squadrons. Note that this list MUST NOT be empty.
     */
    public SquadronSelectionContainer(ActionListener actionListener, List<Squadron> squadrons)
    {
        this.squadrons = squadrons;
        
        setLayout(new GridBagLayout());
        setBorder(new TitledBorder("Select the squadron you want to play"));

        comboBox = new JComboBox(Squadron.Type.values());
        comboBox.addActionListener((ActionListener) this);

        previewImage = new JLabel(new ImageIcon(squadrons.get(0).getImage()));

        button = new JButton("Continue");
        button.addActionListener(actionListener);

        GridBagConstraints c = new GridBagConstraints();

        c.gridx = 0;
        c.gridy = 0;
        add(comboBox, c);

        c.gridy++;
        c.insets = new Insets(4, 0, 0, 0);
        add(previewImage, c);

        c.gridy++;
        add(button, c);
    }

    /**
     */
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == comboBox)
        {
            previewImage.setIcon(new ImageIcon(squadrons.get(comboBox.getSelectedIndex()).getImage()));
            previewImage.repaint();
        }
    }

    /**
     * @return The okay button.
     */
    public Object getButton()
    {
        return button;
    }

    /**
     * @return The selected squadron.
     */
    public Squadron getSelectedSquadron()
    {
        return squadrons.get(comboBox.getSelectedIndex());
    }
}
